package com.io.example.demoparent.mq.work;


import com.io.example.demoparent.mq.util.RabbitMQUtil;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 *  工作模式
 */
public class work02 {

    public static final  String QUEUE_NAME="ack_queue";



    public  static  void workConsumer() throws IOException, TimeoutException {
        boolean autoAck=Boolean.FALSE;
        Channel channel = RabbitMQUtil.getRabbitChannel( );
        channel.basicConsume(QUEUE_NAME,autoAck, (s, delivery) -> {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("消息内容： " + new String(delivery.getBody(), StandardCharsets.UTF_8) );
            //手动应答
            try {
                /*
                   1.消息的标识
                   2.是否批量应答
                 */
                channel.basicAck(delivery.getEnvelope().getDeliveryTag( ),Boolean.FALSE);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }

//            int prefetchCount=1;
//            channel.basicQos(prefetchCount);
        }, s -> {
            System.out.println(s+"消费者取消消费回调接口");
        });

        System.out.println("c2 等到接受消息------" );
    }

    public static void main(String[] args) throws IOException, TimeoutException {
        workConsumer();
    }

}
